<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加任务</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="任务ID"   prop="projectId">
        <el-input  v-model="ruleForm.projectId"  autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-select v-model="ruleForm.taskName"  placeholder="请选择">
          <el-option
              v-for="item in roleListOptions"
              :value="item"
              >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-input  v-model="ruleForm.taskType" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="任务需求" prop="demand">
        <el-input  v-model="ruleForm.demand" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="被派发人"  prop="beDistributeName">
        <el-select v-model="ruleForm.beDistributeName"  placeholder="请选择">
          <el-option
              v-for="item in UserListOptions"
              :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    </div>
</template>
<script>
export default {
  data() {
    return {
      roleListOptions: [],
      UserListOptions:[],
      disListOptions: [],

      ruleForm: {
        taskName:'',taskType:'',projectId:'',demand:'',distributeName:'',beDistributeName:''
      },
      rules: {
        taskName: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 0, max: 15, message: '长度在 0 到 15 个字符', trigger: 'blur' }
        ],
        taskType: [
          { required: true, message: '请输入任务类型', trigger: 'blur' },
          { min: 0, max: 15, message: '长度在 0 到 15 个字符', trigger: 'blur' }
        ],
        projectId: [
          { required: true, message: '请输入任务ID', trigger: 'blur' },
        ],
        demand: [
          { required: true, message: '请输入任务需求', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        beDistributeName: [
          { required: true, message: '请输入派发人', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {

    loadRoleList() {
      console.log('loadRoleList');
      let url = 'http://localhost:8989/projects/select/pro/name';
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}}).
      get(url).then((response) => {
        console.log(response.data);
        let responseBody = response.data;
        this.roleListOptions = responseBody.data;
      });
    },
    loadUserList() {
      console.log('loadUserList');
      let url = 'http://localhost:9878/users';
      console.log('url = ' + url);
      this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}}).
      get(url).then((response) => {
        console.log(response.data);
        let responseBody = response.data;

        this.UserListOptions = responseBody.data;
      });
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url='http://localhost:8989/tasks/add-new';
            console.log('url='+url);
            let formData=this.qs.stringify(this.ruleForm);
            console.log('formData='+formData);
            this.axios.create({'headers': {'Authorization': localStorage.getItem('jwt')}}).post(url,formData).then((response)=>{
              let responseBody=response.data;
              console.log('responseBody=');
              console.log(responseBody);
              if (responseBody.state==20000){
                this.$message({
                  message:'添加任务成功!',
                  type:'success'
                });
                this.resetForm(formName);
              }else {
                this.$message.error(responseBody.message);
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
  },
  mounted() {
    this.loadRoleList();
    this.loadUserList();
  }
}
</script>